/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *    Extract from GDMphotosetup - graphical .gnome2/photo setup program for users
 *
 *    Copyright (C) 2001 Queen of England
 *    Copyright (C) 2005 William Jon McCann <mccann@jhu.edu>
 *    
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *   
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *   
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */

#include <gtk/gtk.h>

#define MAX_PREVIEW_SIZE 128

static GdkPixbuf* create_preview_pixbuf (const gchar *uri);
static void update_preview_cb (GtkFileChooser *chooser);

static char *imagename;

void
add_preview_widget (GtkWidget *widget)
{

  GtkWidget *image;

  image = gtk_image_new ();
  gtk_widget_set_size_request (image, MAX_PREVIEW_SIZE, MAX_PREVIEW_SIZE);

  gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (widget), image);
  gtk_image_set_from_pixbuf (GTK_IMAGE (image), create_preview_pixbuf (imagename));
  g_signal_connect (widget, "update-preview", G_CALLBACK (update_preview_cb), NULL);

}

static GdkPixbuf*
create_preview_pixbuf (const gchar *uri)
{

  GdkPixbuf *pixbuf = NULL;

  if ((uri != NULL) && (uri[0] != '\0'))
    {

      gchar *file = NULL;

      if (g_path_is_absolute (uri) == TRUE)
        {
          file = g_strdup (uri);
        }
      else
        {
          /* URIs are local, because gtk_file_chooser_get_local_only() is true. */
          file = g_filename_from_uri (uri, NULL, NULL);
        }

      if (file != NULL)
        {

          GdkPixbufFormat *info;
          gint width;
          gint height;

          info = gdk_pixbuf_get_file_info (file, &width, &height);

          if (width > MAX_PREVIEW_SIZE || height > MAX_PREVIEW_SIZE)
            {
              pixbuf = gdk_pixbuf_new_from_file_at_size (file, MAX_PREVIEW_SIZE, MAX_PREVIEW_SIZE, NULL);
            }
          else
            {
              pixbuf = gdk_pixbuf_new_from_file (file, NULL);
            }
          g_free (file);
        }
    }

  return pixbuf;
}

static void
update_preview_cb (GtkFileChooser *chooser)
{

  GtkWidget *image;
  gchar *uri;

  image = gtk_file_chooser_get_preview_widget (GTK_FILE_CHOOSER (chooser));
  uri = gtk_file_chooser_get_preview_uri (chooser);

  if (uri != NULL)
    {

      GdkPixbuf *pixbuf = NULL;

      pixbuf = create_preview_pixbuf (uri);

      if (pixbuf)
        {
          gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
          g_object_unref (pixbuf);
        }
      else
        {
          gtk_image_set_from_stock (GTK_IMAGE (image), GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
        }
    }

  gtk_file_chooser_set_preview_widget_active (chooser, TRUE);

}
